package com.zhj.network.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @PackageName:com.zhj.network.tcp
 * @ClassName:TcpClientDemo1
 * @auter: 朱慧杰
 * @date:2020/7/17 0017 18:45
 **/
public class TcpClientDemo1 {

    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;

        try {
            // 1.服务端地址
            InetAddress serverIP = InetAddress.getByName("localhost");
            // 2.端口号
            int port = 9999;
            // 3.创建一个连接
            socket =  new Socket(serverIP, port);
            // 4.发送消息IO流
            os = socket.getOutputStream();
            os.write("你好啊，同学！".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
